package com.wangjie.simple;

import java.util.HashMap;
import java.util.Map;

/**
 * @author jieshao
 * @date 2022/5/27 11:18
 *
 * 219. 存在重复元素 II
 */
public class Test05 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 1};
        boolean res = containsNearbyDuplicate(arr, 3);
        System.out.println(res);
    }

    public static boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int num = nums[i];
            if (map.containsKey(num) && (i - map.get(num) <= k)) {
                return true;
            }
            map.put(num, i);
        }
        return false;
    }
}